package com.jucai.pppeiyouclient.adapter;

import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.base.QuickFragmentAdapter;
import com.jucai.pppeiyouclient.weidget.BadgeView;

import java.util.List;

/**
 * Created by Kuky on 2017-05-02.
 */

public class BadgeTabAdapter extends QuickFragmentAdapter {
    private Context mContext;
    private List<Integer> mBadgeCountList;

    public void setBadgeCounts(List<Integer> badgeCounts) {
        this.mBadgeCountList = badgeCounts;
        notifyDataSetChanged();
    }

    public BadgeTabAdapter(Context context, FragmentManager fm, List list, String[] titles, List<Integer> badgeCountList) {
        super(fm, list, titles);
        this.mContext = context;
        this.mBadgeCountList = badgeCountList;
    }

    public View getTabItemView(int position) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.item_badge_tab_layout, null);
        TextView textView = (TextView) view.findViewById(R.id.badge_text);
        textView.setText(titles[position]);
        View target = view.findViewById(R.id.target_badge);

        BadgeView badgeView = new BadgeView(mContext);
        badgeView.setTargetView(target);
        badgeView.setBadgeMargin(0, 6, 10, 0);
        badgeView.setTextSize(10);

        if (!mBadgeCountList.isEmpty()) {
            badgeView.setText(formatBadgeNumber(mBadgeCountList.get(position)));
        }

        return view;
    }

    public static String formatBadgeNumber(int value) {
        if (value <= 0) {
            return null;
        }

        if (value < 100) {
            return Integer.toString(value);
        }

        return "99+";
    }
}
